// 防抖函数
export const debounce = (func, delay) => {
  let timer
  return function(...args) {
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      func.apply(this, args)
      clearTimeout(timer)
    }, delay)
  }
}
// 获取localStorage
export function getStorage(key) {
  return localStorage.getItem(key)
}

// 重置localStorage
export function setStorage(key, value) {
  localStorage.setItem(key, JSON.stringify(value))
}

// 移除localStorage
export function removeStorage(key) {
  localStorage.removeItem(key)
}

// 替换邮箱字符
export const replaceEmail = (email) => {
  if (String(email).indexOf('@') > 0) {
    let str = email.split('@'),
      _s = ''
    if (str[0].length > 1) {
      for (let i = 0; i < str[0].length - 1; i++) {
        _s += '*'
      }
    } else {
      _s = '*'
    }
    var new_email = str[0].substr(0, 1) + _s + str[0].substr(str[0].length - 1, str[0].length) + '@' + str[1]
  }
  return new_email
}
// 替换手机号
export const replaceMobile = (mobile) => {
  return mobile.substring(0, 3) + '******' + mobile.substr(mobile.length - 2)
}
// 替换姓名 或者全部字符为加密
export const replaceName = (name, num = 1) => {
  if (!name) return
  if (typeof name == 'number') {
    name = name.toString()
  }
  let nameSub = name.substring(0, num)
  for (let i = 0; i < name.length - num == 1 ? 1 : 0; i++) {
    nameSub = nameSub.concat('*')
  }
  return nameSub
}

// 时间时区转换
export const currencyTime = (date) => {
  // const newTimes=times.getFullYear() + '-' + (times.getMonth() + 1) + '-' + times.getDate() + ' ' + times.getHours() + ':' + times.getMinutes() + ':' + times.getSeconds();
  var y = date.getFullYear()
  var m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  var d = date.getDate()
  d = d < 10 ? '0' + d : d
  var h = date.getHours()
  h = h < 10 ? '0' + h : h
  var minute = date.getMinutes()
  var second = date.getSeconds()
  minute = minute < 10 ? '0' + minute : minute
  second = second < 10 ? '0' + second : second
  const newTimes = y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
  return newTimes
}

const cookieDomain = 'domain=.zicber.com'

// 获取cookie

export const getCookie = (key) => {
  if (!key) {
    return null
  }
  const cookieResult = document.cookie.match(new RegExp('(?:^|\\s+|;+)' + key + '=([^;]*)'))
  return cookieResult ? cookieResult[1] : null
}

// 设置cookie  expires是天数

export const setCookie = (key, val, expires = 7) => {
  let expiresTime = null

  if (expires) {
    expiresTime = new Date()

    expiresTime.setMilliseconds(expiresTime.getMilliseconds() + expires * 864e5)

    expiresTime = expiresTime.toUTCString()
  }

  document.cookie = [encodeURIComponent(key), '=', val, expiresTime ? '; expires=' + expiresTime : '', ';', 'path=/;', cookieDomain].join('')
}

// 删除cookie

export const removeCookie = (key) => {
  if (!key) {
    return
  }
  document.cookie = key + '=; expires=Mon, 26 Jul 1997 05:00:00 GMT; path=/; ' + cookieDomain
}

// 切换主题颜色

// 变量前缀
export const colorChange = (e) => {
  // localStorage.setItem('themeColor', e)
  // e就是选择了的颜色
  const pre = '--el-color-primary'
  // 白色混合色
  const mixWhite = '#ffffff'
  // 黑色混合色
  const mixBlack = '#000000'
  const el = document.documentElement
  el.style.setProperty(pre, e)
  // 这里是覆盖原有颜色的核心代码
  for (let i = 1; i <= 9; i += 1) {
    el.style.setProperty(`${pre}-light-${i}`, mix(e, mixWhite, i * 0.1))
    el.style.setProperty(`${pre}-dark-${i}`, mix(e, mixBlack, i * 0.1))
  }
  el.style.setProperty('--el-color-primary-dark', mix(e, mixBlack, 0.1))

  document.getElementsByTagName('body')[0].style.setProperty('--global-Bg', e)
}
const mix = (color1, color2, weight) => {
  weight = Math.max(Math.min(Number(weight), 1), 0)
  let r1 = parseInt(color1.substring(1, 3), 16)
  let g1 = parseInt(color1.substring(3, 5), 16)
  let b1 = parseInt(color1.substring(5, 7), 16)
  let r2 = parseInt(color2.substring(1, 3), 16)
  let g2 = parseInt(color2.substring(3, 5), 16)
  let b2 = parseInt(color2.substring(5, 7), 16)
  let r = Math.round(r1 * (1 - weight) + r2 * weight)
  let g = Math.round(g1 * (1 - weight) + g2 * weight)
  let b = Math.round(b1 * (1 - weight) + b2 * weight)
  r = ('0' + (r || 0).toString(16)).slice(-2)
  g = ('0' + (g || 0).toString(16)).slice(-2)
  b = ('0' + (b || 0).toString(16)).slice(-2)
  return '#' + r + g + b
}

export function formatTime(time, option) {
  time = +time * 1000
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
  }
}

// 下载附件
export function downLoadFile(item) {
  let url = updateQueryParams(item.path, 'fileName', item.name)
  let a = document.createElement('a')
  a.href = updateQueryParams(url, 'access_token', getAccessToken())
  a.download = item.name
  a.click()
}
export function updateQueryParams(uri, key, value) {
  if (!value) {
    return uri
  }
  var re = new RegExp('([?&])' + key + '=.*?(&|$)', 'i')
  var separator = uri.indexOf('?') !== -1 ? '&' : '?'
  if (uri.match(re)) {
    return uri.replace(re, '$1' + key + '=' + value + '$2')
  } else {
    return uri + separator + key + '=' + value
  }
}
// 下载导出表格
export function download(href, name) {
  const a = document.createElement('a')
  a.setAttribute('href', href)
  a.setAttribute('download', name)
  a.click()
}
